package esmp.jtt808.model.terminal;

import common.utils.DataPackUtil;
import esmp.jtt808.model.Decoder;

import java.time.Instant;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.Date;

/**
 * Created by dave on 18-7-21 上午12:06.
 */
public class LocationReport implements Decoder<LocationReport> {
    private static final DateTimeFormatter DATE_FORMAT = DateTimeFormatter.ofPattern("yyMMddHHmmss");
    private int alarmFlag;
    private int flag;
    private boolean located; // 是否定位
    private double lat;
    private double lng;
    private int height;     // 海拔高度,单位为米( m)
    private int speed;      // 1/10km/h
    private int direction;  // 0 ~ 359,正北为 0,顺时针
    private Date time;      // GMT + 8 时间

    @Override
    public LocationReport decode(byte[] bytes, int start, int end) {
        alarmFlag = (int) DataPackUtil.readDWord(bytes, start);
        flag = (int) DataPackUtil.readDWord(bytes, start + 4);
        located = (flag & 0x10) > 0;
        lat = DataPackUtil.readDWord(bytes, start + 8) / (double)1000_000;
        lng = DataPackUtil.readDWord(bytes, start + 12) / (double)1000_000;
        height = DataPackUtil.readWord(bytes, start + 16);
        speed = DataPackUtil.readWord(bytes, start + 18);
        direction = DataPackUtil.readWord(bytes, start + 20);

        try {
            String st = DataPackUtil.readBCD(bytes, start + 22, 6);
            time = Date.from(Instant.from(DATE_FORMAT.parse(st)));
        } catch (DateTimeParseException e) {
            time = new Date();
        }
        return this;
    }

    public int getAlarmFlag() {
        return alarmFlag;
    }

    public void setAlarmFlag(int alarmFlag) {
        this.alarmFlag = alarmFlag;
    }

    public int getFlag() {
        return flag;
    }

    public void setFlag(int flag) {
        this.flag = flag;
    }

    public boolean isLocated() {
        return located;
    }

    public void setLocated(boolean located) {
        this.located = located;
    }

    public double getLat() {
        return lat;
    }

    public void setLat(double lat) {
        this.lat = lat;
    }

    public double getLng() {
        return lng;
    }

    public void setLng(double lng) {
        this.lng = lng;
    }

    public int getHeight() {
        return height;
    }

    public void setHeight(int height) {
        this.height = height;
    }

    public int getSpeed() {
        return speed;
    }

    public void setSpeed(int speed) {
        this.speed = speed;
    }

    public int getDirection() {
        return direction;
    }

    public void setDirection(int direction) {
        this.direction = direction;
    }

    public Date getTime() {
        return time;
    }

    public void setTime(Date time) {
        this.time = time;
    }
}
